package com.yn.xgame.logic.handler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yn.xgame.DBHandler;
import com.yn.xgame.logic.GamePlayer;
import com.yn.xgame.logic.util.CheckUtils;
import com.yn.xgame.logic.util.MD5Utils;
import com.yn.xgame.message.C2GPbRedirectMessage;

public class UserBaseInfoHandler extends DBHandler {
	
	private Logger logger=LoggerFactory.getLogger(UserBaseInfoHandler.class);
	
	public UserBaseInfoHandler(QueryRunner runner,Connection conn) {
		m_runner=runner;
		m_conn=conn;
	}
	
	@Override
	public Logger logger() {
		return logger;
	}
	
	public int changePwd(C2GPbRedirectMessage msg, GamePlayer player){
		String oldPwd=msg.getPb().getChangeUserBaseInfoReq().getOldPwd();
		String newPwd=msg.getPb().getChangeUserBaseInfoReq().getNewPwd();
		if(!StringUtils.isNoneBlank(oldPwd,newPwd)){
			return 4;
		}
		try{
			oldPwd=MD5Utils.encryptTohex(oldPwd);
			if(!oldPwd.equals(player.getPwd())){
				return 3;
			}
			newPwd=MD5Utils.encryptTohex(newPwd);
			String sql="update user set user_pwd='"+newPwd+"' where user_id="+player.getUserId();
			if(saveUpdate(sql)>0){
				player.setPwd(newPwd);
				return 1;
			}
		}catch(SQLException e){
			logger.error(e.getMessage());
			return 5;
		}
		return 0;
	}
	
	public int resetPwd(C2GPbRedirectMessage msg, GamePlayer player){
		String captcha=msg.getPb().getChangeUserBaseInfoReq().getCaptcha();
		String newPwd=msg.getPb().getChangeUserBaseInfoReq().getNewPwd();
		if(StringUtils.isBlank(captcha)||StringUtils.isBlank(newPwd)){
			return 4;
		}
		try{
			newPwd=MD5Utils.encryptTohex(newPwd);
			String sql="update user set user_pwd='"+newPwd+"' where user_id="+player.getUserId();
			if(saveUpdate(sql)>0){
				player.setPwd(newPwd);
				return 1;
			}
		}catch(Exception e){
			logger.error(e.getMessage());
			return 5;
		}
		return 0; 
	}
	
	public int changeIconOrNickname(C2GPbRedirectMessage msg, GamePlayer player){
		int userIcon=msg.getPb().getChangeUserBaseInfoReq().getUserIcon();
		String nickname=msg.getPb().getChangeUserBaseInfoReq().getNickname();
		String sql="update user set";
		boolean hasIcon=false;
		boolean hasNickname=false;
		if(userIcon!=0){
			sql+=" user_icon="+userIcon;
			hasIcon=true;
		}
		if(StringUtils.isNotBlank(nickname)){
			if(CheckUtils.checkNickName(nickname)){
				sql+=" user_nickname=?";
				hasNickname=true;
			}else{
				return 4;
			}
		}
		if(!sql.endsWith("set")){
			return 4;
		}
		sql+=" where user_id="+player.getUserId();
		try{
			m_conn.setAutoCommit(false);
			m_conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
			int result=0;
			if(!hasNickname){
				result=saveUpdate(sql);
				m_conn.commit();
				if(result>0){
					player.setUserIcon(userIcon);
					return 1;
				}
			}else{
				Map<String,Object> dbResult=queryOne("select count(user_id) as c from where user_nickname=? and user_id !="+player.getUserId(),new Object[]{nickname});
				long count=getLong(dbResult,"c");
				if(count>0){
					return 2;
				}
				result=saveUpdate(sql,new Object[]{nickname});
				m_conn.commit();
				if(result>0){
					if(hasIcon){
						player.setUserIcon(userIcon);
					}
					player.setUserNickName(nickname);
					return 1;
				}
			}
		}catch(SQLException e){
			logger.error(e.getMessage());
			try{
				m_conn.rollback();
			}catch(SQLException e1){
				logger.error(e1.getMessage());
			}
			return 5;
		}finally{
			try {
				m_conn.setAutoCommit(true);
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
		return 0;
	}
}
